package com.caissa.springboot.starter.permission.service;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 在request请求的header、attribute中获取用户信息，传递给下一个feign
 *
 * @Author yuzhongbo
 * @date 2020/5/12 PM2:11
 */
@Slf4j
public class FeignUicRequestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        // 获取当前的request对象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();

            // 从header中获取Token
            template.header(TokenConstant.xToken, request.getHeader(TokenConstant.xToken));
            template.header(TokenConstant.xTokenType, request.getHeader(TokenConstant.xTokenType));
            template.header(TokenConstant.permissionKey, request.getHeader(TokenConstant.permissionKey));

            template.header(TokenConstant.xId, request.getHeader(TokenConstant.xId));
            template.header(TokenConstant.xPhone, request.getHeader(TokenConstant.xPhone));
            template.header(TokenConstant.xBtSystem, request.getHeader(TokenConstant.xBtSystem));
            template.header(TokenConstant.xLoginCode, request.getHeader(TokenConstant.xLoginCode));
            template.header(TokenConstant.xName, request.getHeader(TokenConstant.xName));
        }
    }
}